package org.zeurox.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.springframework.dao.DataAccessException;
import org.zeurox.model.Login;
import org.zeurox.service.IUserService;

@ManagedBean(name="userMB")
@RequestScoped
public class UserManagedBean implements Serializable {
	private static final long serialVersionUID = 1L;
	private static final String SUCCESS = "success";
	private static final String ERROR   = "error";
	
	//Spring User Service is injected...
	@ManagedProperty(value="#{UserService}")
	IUserService userService;
	
	List<Login> userList;

	private Integer id;
	private String name;
	private String surname;
	
	public String addUser() {
		  try {
		   Login user = new Login();
		   user.setId(getId());
		   user.setNombre(getName());
		   user.setPassword(getSurname());
		   getUserService().addUser(user);
		   return SUCCESS;
		  } catch (DataAccessException e) {
		   e.printStackTrace();
		  }  

	  return ERROR;
	 }
	
	 public void reset() {
		  this.setId(0);
		  this.setName("");
		  this.setSurname("");
	 }
	
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	public List<Login> getUserList() {
		userList = new ArrayList<Login>();
		userList.addAll(getUserService().getUsers());
		return userList;
	}
	public void setUserList(List<Login> userList) {
		this.userList = userList;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSurname() {
		return surname;
	}
	public void setSurname(String surname) {
		this.surname = surname;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public static String getSuccess() {
		return SUCCESS;
	}
	public static String getError() {
		return ERROR;
	}


	
	
}
